﻿

/***********************************************************************************************************
* Color Scheme:
* 
* White:  R:255 G:255 B:255 A:255 =      Ways            (Both can pass)        
* 
* Blue:   R:0 G:0 B:255 A:255     =      SpiritWalls     (Energy Walls the Ghost can't pass, the body can)
*
* Yellow: R:255 G:255 B:0 A:255   =      Normal Doors    (Ghost can pass, Body can't)
* Cyan:   R:0 G:255 B:255 A:255   =      Gap             (Ghost can pass, body can't)  V
*
* Brown:  R:165 G:42 B:42 A:255   =      Level_3 Doors     (Both can't pass)
* Black:  R:0 G:0 B:0 A:255       =      Walls           (The actualLevel Walls, both can't pass)
************************************************************************************************************/


using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PyramidShadow
{
    class Kollision 
    {
        private int[] ghostEdges,bodyEdges;
        private Color[] colHolderGhost, colHolderBody, ghostColors, bodyColors;
        private Level level;
        public Kollision(Level level)
        {
            this.level = level;
            //init the Color Holder for each player
            colHolderBody = new Color[level.body.BoundingBox.Height * level.body.BoundingBox.Width];
            colHolderGhost = new Color[level.ghost.BoundingBox.Height * level.ghost.BoundingBox.Width];

            //Set Colors for the players which return collision

            ghostColors = new Color[3] { Color.Black, Color.Blue, Color.Brown };
            bodyColors = new Color[4] { Color.Black, Color.Brown, Color.Cyan, Color.Yellow };
        }

        public bool intersection(Rectangle box_1, Rectangle box_2)
        {
            return box_1.Intersects(box_2);
        }

        // To be done: Ne Abfrage das die übergebenen Werte nicht auserhalb des Fensters liegen
        //Kollision for the player
        public bool collide(Rectangle box, BoxType typ)
        {
            //Avoid OutOfBounds Exception
            if (box.X < 0 ||
                box.Y < 0 ||
               (box.X + box.Width) >= WindowSize.WINDOW_WIDTH ||
               (box.Y + box.Height) >= WindowSize.WINDOW_HEIGHT)
                    return true;
            int end = box.Width * box.Height;
            Color[] colHolder;
            if (typ == BoxType.Body || typ == BoxType.Object)
                colHolder = colHolderBody;
            else
                colHolder = colHolderGhost;

            //get the colors for every pixel  in the box
            level.map.ColorMap.GetData(0, box, colHolder, 0, colHolder.Length);
            if (check(0, typ) || check(box.Width - 1, typ) || check((end - box.Width) - 1, typ) || check(end - 1, typ))
                return true;

            return false;
        }
        
        private Boolean check(int pos, BoxType typ)
        {
            if (typ == BoxType.Body)
            {
                for (int j = 0; j < bodyColors.Length; j++)
                {
                    if (colHolderBody[pos] == bodyColors[j])
                        return true;
                }
            }
            else
            {
                for (int j = 0; j < ghostColors.Length; j++)
                {
                    if (colHolderGhost[pos] == ghostColors[j])
                        return true;
                }
            }
            return false;
        }

    }
}
